package cn.pomelo.secondskill.controller;

import cn.pomelo.secondskill.common.AjaxResult;
import cn.pomelo.secondskill.model.UserInfo;
import cn.pomelo.secondskill.service.UserInfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户信息
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @RequestMapping("/login")
    public AjaxResult login(String openid) {
        UserInfo userInfo = userInfoService.selectByOpenid(openid);
        if (userInfo != null) {
            return AjaxResult.success(userInfo);
        }
        UserInfo login = userInfoService.login(openid);
        return login == null ? AjaxResult.failure("登录失败") : AjaxResult.success(login);
    }

    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody UserInfo userInfo) {
        int i = userInfoService.updateByPrimaryKeySelective(userInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.failure();
    }

}
